define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dojo/store/JsonRest'
   ],
   function(declare, lang, JsonRest)
   {
      return declare([JsonRest],
      {
         query:'root',
         rootId:'Root',
         idProperty:'Id',
         deferItemLoadingUntilExpand:true,
         childrenAttrs:['children'],
         mayHaveChildren:function (item) {
            return 'children' in item;
         },
         getRoot:function (onItem, onError) {
            this.get(this.rootId).then(onItem, onError);
         },
         getLabel:function (object) {
            // just get the name
            return object.Name;
         },
         getChildren:function (object, onComplete, onError)
         {
            if (object.children && object.children.length > 0)
            {
               onComplete(object.children);
            }
            else if(object.children === true)
            {
               this.get(object.Id).then(function (fullObject) {
                  object.children = fullObject.children;
                  onComplete(fullObject.children);
               }, onError);
            }
         },
         put:function (object, options) {
            this.onChildrenChange(object, object.children);
            this.onChange(object);
            return this.inherited(arguments);
            //return dojo.store.JsonRest.prototype.put.apply(this, arguments);
         },
         onChildrenChange:function (parent, children) {
            // fired when the set of children for an object change
         },
         onChange:function (object) {
            // fired when the properties of an object change
         }
      });
   }
);
